{% set monasca_transform_source = fetch_source('https://tarballs.openstack.org/monasca-transform/monasca-transform-master.tar.gz') %}
{% set pypi_name = 'monasca-transform' %}
{% set pypi_tar_name = 'monasca_transform' %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
{% set spark_req_version = '1.6.3' %}

%global sname {{ pypi_name }}
%define username            monasca-transform
%define groupname           monasca
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        A transformation and aggregation engine for Monasca
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://wiki.openstack.org/wiki/Monasca/Transform
Source0:        {{ monasca_transform_source|basename }}
Source1:        openstack-monasca-transform.service
BuildRequires:  crudini
BuildRequires:  fdupes
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('PyMySQL') }}
BuildRequires:  {{ py3('SQLAlchemy') }}
BuildRequires:  {{ py3('kazoo') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('monasca-common') }}
BuildRequires:  {{ py3('nose') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.service') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('psutil') }}
BuildRequires:  {{ py3('six') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('stevedore') }}
BuildRequires:  {{ py3('tooz') }}
BuildRequires:  zip
#BuildRequires:  spark == 1.6.3
Requires:       python3-%{sname} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
Requires(pre):  pwdutils
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun):   systemd
Requires(preun):    systemd
%endif

%description
A transformation and aggregation engine for Monasca.  Collects, groups and
aggregates existing individual Monasca metrics according to business
requirements and publishes new transformed (derived) metrics to the Monasca
Kafka queue.
Requires an installation of Apache Spark {{ spark_req_version }}, either on
this server or another, and configured in %{_sysconfdir}/monasca-transform.conf
as spark_master_list.

%package -n python3-%{sname}
Summary:        Python core module for monasca-transform
Group:          Development/Languages/Python
Requires:       {{ py3('PyMySQL') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('kazoo') }}
Requires:       {{ py3('monasca-common') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('tooz') }}
#Requires:       spark == 1.6.3

%description -n python3-%{sname}
A transformation and aggregation engine for Monasca.  Collects, groups and
aggregates existing individual Monasca metrics according to business
requirements and publishes new transformed (derived) metrics to the Monasca
Kafka queue.
Requires an installation of Apache Spark {{ spark_req_version }}, either on this server or
another, and configured in %{_sysconfdir}/monasca-transform.conf as spark_master_list.

%prep
# Unfortunately again, .tgz has _ in directory name so can't use sname
%autosetup -n {{ pypi_tar_name }}-{{ upstream_version }}
%py_req_cleanup
# Add entry point for service startup by appending console_scripts after [entry_points].
sed -i '/\[entry_points\]/aconsole_scripts =\n    monasca-transform = monasca_transform.service.transform_service:main_service\n' setup.cfg

%build
export LANG=en_US.UTF-8
%{py3_build}

# copy, compile and create a zip file to be fed to spark
python3 -m compileall monasca_transform
zip -r %{sname}.zip monasca_transform -i '*.pyc'

%install
export LANG=en_US.UTF-8
%{py3_install}

install -d -m 755 %{buildroot}%{_sysconfdir}/monasca
install -d -m 750 %{buildroot}%{_sysconfdir}/monasca_transform
install -d -m 750 %{buildroot}%{_sysconfdir}/monasca_transform/monasca_transform.conf.d
install -D -m 640 etc/monasca-transform.conf %{buildroot}%{_sysconfdir}/monasca-transform.conf

install -d -m 750 %{buildroot}%{_var}/log/monasca
install -d -m 750 %{buildroot}%{_var}/log/monasca/transform

# systemd unit file
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_unitdir}/%{name}.service
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir}
ln -sr %{buildroot}%{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}
%endif

install -d -m 750 %{buildroot}%{_datadir}/%{sname}
install -D -m 644 %{sname}.zip %{buildroot}%{_datadir}/%{sname}/%{sname}.zip

# reconfigure for rpm install
%define mon_trans_conf %{buildroot}%{_sysconfdir}/monasca-transform.conf
crudini --set %{mon_trans_conf} service service_log_path %{_var}/log/monasca/transform/
crudini --del %{mon_trans_conf} service setup_file
crudini --set %{mon_trans_conf} service spark_driver %{python3_sitelib}/monasca_transform/driver/mon_metrics_kafka.py
# disable spark event logging - mon-trans only keeps 10 jobs so history not needed
crudini --set %{mon_trans_conf} service spark_event_logging_enabled false
# NOTE: Special handling for spark_jars_list will need to be done after install
crudini --set %{mon_trans_conf} service spark_home $"$spark_home"
crudini --set %{mon_trans_conf} service work_dir %{_rundir}/monasca/monasca-transform
crudini --set %{mon_trans_conf} service spark_master_list spark://localhost:7078
# for the .zip file created above
crudini --set %{mon_trans_conf} service spark_python_files %{_datadir}/%{sname}/%{sname}.zip

%fdupes %{buildroot}%{python3_sitelib}

%check
# automated tests need to be restricted, as functional tests require running Spark
find . -type f -name *.pyc -delete
python3 -m stestr.cli run

%pre
# create user and groups
%openstack_pre_user_group_create %{username} %{groupname} /sbin/nologin

%post
%systemd_post %{name}.service

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun %{name}.service

%files
%license LICENSE
%doc README.rst ChangeLog
%dir %attr(-, root, %{groupname}) %{_sysconfdir}/monasca
%config(noreplace) %attr(0640, root, %{groupname}) %{_sysconfdir}/monasca-transform.conf
%{_unitdir}/%{name}.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}
%endif
%{_bindir}/monasca-transform
%dir %attr(0750, root, %{groupname}) %{_var}/log/monasca
%dir %attr(0750, %{username}, %{groupname}) %{_var}/log/monasca/transform

%files -n python3-{{ pypi_name }}
%license LICENSE
%{python3_sitelib}/monasca_transform
%{python3_sitelib}/*.egg-info
%dir %attr(0750, %{username}, %{groupname}) %{_datadir}/%{sname}
%{_datadir}/%{sname}/%{sname}.zip

%changelog
